#####################################
# POLITIES AND RULERS: TABLE
# 
#
# Replication Material for:
# Continuity or Change? (In)direct Rule in British and French Colonial Africa
# 
# Carl Mueller-Crepon, 2020
# International Organization
#
# File Description:
# Produces Appendix Table A5: Overview over all polities in sample
#
# Called from scripts/polities/analysis.R 
#
##################################

# Subset data
polities.df <- polity.yrs.df[polity.yrs.df$action.type != "decolonization" &
                               (polity.yrs.df$col.brit == 1 | polity.yrs.df$col.frnc == 1) &
                               !is.na(polity.yrs.df$start.col) & !is.na(polity.yrs.df$stop.col),]

# Take last entry by polity
uni.polities.df <- polities.df[order(polities.df$polity, polities.df$year),]
uni.polities.df <- uni.polities.df[!duplicated(uni.polities.df$polity.id, fromLast = T),]

# Subset to reelvant columns
uni.polities.df <- unique(uni.polities.df[, c("polity","polity.id", "year", "capital.id",
                                          "col.brit", "col.frnc" ,
                                          "start.year", 
                                          "min.col.year" ,"ind.year", "end.max" )])

# Add capitals

## Unique capitals
uni.capitals <- read.csv(file.path("data/polities_raw", "capitals_final.csv"), 
                         stringsAsFactors = F)[, c("capital.id","capital.name")]

## Join
uni.polities.df <- plyr::join(uni.polities.df, uni.capitals, by = "capital.id", type = "left")

## Subset to non-missing capitals
uni.polities.df <- uni.polities.df[!is.na(uni.polities.df$capital.name),]

## Check for duplicates
stopifnot(!any(duplicated(uni.polities.df$polity)))

# Finalize table

## Colonizer
uni.polities.df$colonizer <- ifelse(uni.polities.df$col.brit == 1, "Great Britain", "France")

## Colonial end of polity
uni.polities.df$end <- ifelse(uni.polities.df$end.max < uni.polities.df$ind.year | 
                               is.na(uni.polities.df$ind.year),
                              paste(uni.polities.df$end.max, "(E)"),
                              paste(uni.polities.df$ind.year, "(I)")) 


# Print table

## Prepare
print.df <- uni.polities.df[, c("polity", "capital.name", "colonizer", "min.col.year", "end")]
colnames <- c("Polity", "Capital", "Colonizer", "Colonized in", "End year")

## Header
head <- paste("\\begin{longtable}{ p{.20\\textwidth} p{.20\\textwidth} p{.20\\textwidth} p{.10\\textwidth}  p{.10\\textwidth}} ",
              "\\caption{Polities colonized by France or Great Britain}",
              "\\label{politiesall}",
              " \\\\[-1.8ex]  \\hline  \\hline  \\\\[-1.8ex] ", 
              paste(paste0("\\multicolumn{1}{l}{", colnames, "}"), collapse = "&"),
              "\\\\ \\hline \\\\[-1.8ex] " )

## Main
main <- paste(apply(print.df, 1, function(x){
  paste(paste(x, collapse = " & "), "\\\\[-3.2ex] \\\\ \n")
}), collapse = "")

## Footer
foot <- paste("\\hline \\hline \\\\[-1.8ex] ",
              "\\multicolumn{5}{l}{\\parbox[t]{0.95\\textwidth}{\\textit{Notes:} ",
              "Colonizing powers and dates only refer to colonization by France and Great Britain. ",
              "The last column refers to the last year of a polity in the sample. ",
              "(E) refers to the end of a line of succession during the colonial period.",
              "(I) refers to the independence of the colony whithin which a polity's capital is located.",
              "}}",
              "\\end{longtable}")



## Combine
tab.tex <- paste(head, "\n", main, "\n", foot)


## Save
writeLines(tab.tex, con = file.path(tab.path, paste0("politiesall", ".tex")))
